package site.wenneng.dev.objs.dto.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class Result<T> {

    private Integer code;
    private String msg;
    private T data;

    public static <T> Result<T> ok() {
        return new Result<>(200, "OK", null);
    }

    public static <T> Result<T> ok(T data) {
        return new Result<>(200, "OK", data);
    }

    public static <T> Result<PageDTO<T>> ok(IPage<T> data) {
        PageDTO<T> pageDTO = PageDTO.<T>builder()
                .page(data.getCurrent())
                .pageSize(data.getSize())
                .total(data.getTotal())
                .rows(data.getRecords()).build();
        return new Result<>(200, "OK", pageDTO);
    }

    public static <T> Result<T> fail(int code, String msg) {
        return new Result<>(code, msg, null);
    }
}
